package com.sskj.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sskj.api.domain.SysUser;
import com.sskj.api.domain.Role;
import com.sskj.api.domain.UserRole;
import com.sskj.api.mapper.UserRoleMapper;
import com.sskj.api.service.IUserRoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class UserRoleService extends ServiceImpl<UserRoleMapper, UserRole> implements IUserRoleService {

    @Override
    public Long countByRoleId(Long roleId) {
        LambdaQueryWrapper<UserRole> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(UserRole::getRoleId, roleId);
        return count(lambdaQueryWrapper);
    }

    @Override
    public void bindRoles(Long userId, List<Long> roleIds) {
        if (roleIds != null && !roleIds.isEmpty()) {
            LambdaQueryWrapper<UserRole> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(UserRole::getUserId, userId);
            remove(lambdaQueryWrapper);

            List<UserRole> userRoles = new ArrayList<>();
            for (Long roleId : roleIds) {
                UserRole userRole = new UserRole();
                userRole.setUserId(userId);
                userRole.setRoleId(roleId);
                userRole.setCreateTime(LocalDateTime.now());

                userRoles.add(userRole);
            }
            saveBatch(userRoles);
        }
    }

    @Override
    public void bindUserIds(Long roleId, List<Long> userIds) {
        List<UserRole> insertUserRoles = new ArrayList<>();

        if (userIds != null && !userIds.isEmpty()) {
            for (Long userId : userIds) {
                if (get(userId, roleId) == null) {
                    UserRole userRole = new UserRole();
                    userRole.setUserId(userId);
                    userRole.setRoleId(roleId);
                    userRole.setCreateTime(LocalDateTime.now());

                    insertUserRoles.add(userRole);
                }
            }
        }

        if (!insertUserRoles.isEmpty()) {
            saveBatch(insertUserRoles);
        }
    }

    @Override
    public IPage<SysUser> pageListByRoleId(Long roleId, Long pageNum, Long pageSize) {
        IPage<UserRole> pageConfig = new Page<>(pageNum, pageSize);
        return baseMapper.bindUserIds(pageConfig, roleId);
    }

    @Override
    public List<Role> listByUserId(Long userId) {
        return baseMapper.listByUserId(userId);
    }

    @Override
    public void unBind(Long id) {
        removeById(id);
    }

    @Override
    public Boolean isAdmin(Long userId) {
        LambdaQueryWrapper<UserRole> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(UserRole::getUserId, userId)
                .eq(UserRole::getRoleId, 1L);
        return getOne(lambdaQueryWrapper) != null;
    }

    /**
     *
     * @param userId 用户ID
     * @param roleId 角色ID
     * @return 绑定关系
     */
    private UserRole get(Long userId, Long roleId) {
        LambdaQueryWrapper<UserRole> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(UserRole::getUserId, userId)
                .eq(UserRole::getRoleId, roleId);
        return getOne(lambdaQueryWrapper);
    }
}
